package com.zy.ipanalysis.controller;

import com.zy.ipanalysis.model.IspMask;
import com.zy.ipanalysis.service.IspMaskService;
import com.zy.ipanalysis.service.IspService;
import com.zy.commom.utils.IpUtil;
import com.zy.commom.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.util.Scanner;

/**
 * Created by ZYDR on 2015/12/27.
 */
@Controller
@RequestMapping("/ispMask")
public class IspMaskController {
    @Autowired
    IspMaskService ispMaskService;
    @Autowired
    IspService ispService;
    @RequestMapping("/import")
    public String importData(Model model){
        System.out.println("start");
        String basePath="C:\\Users\\ZYDR\\Desktop\\";//TODO
        String filePath;
        int count=0;
        int total=0;
        for (Isps isps:Isps.values()){
            filePath=basePath+isps+".txt";
            System.out.println(filePath+"*******");
            int ispId=ispService.getIspId(isps.toString());
            try {
                Scanner scanner=new Scanner(new File(filePath));
                while (scanner.hasNextLine()){
                    total++;
                    String[] temp=scanner.nextLine().trim().split("/");//不存在则插入
                    if(ispMaskService.queryByNetId(IpUtil.ip2Int(temp[0]))==null){
                        IspMask ispMask=new IspMask();
                        ispMask.setIspId(ispId);
                        ispMask.setMaskChar(temp[1]);
                        ispMask.setNetIdChar(temp[0]);
                        ispMask.setMask(IpUtil.ip2Int(temp[1]));
                        ispMask.setNetId(IpUtil.ip2Int(temp[0]));
                        ispMaskService.insert(ispMask);
                        count++;
                        System.out.println(count+"*******");
                    }
                    else {
                        System.out.println(temp[0]);
                    }
                }
            }
            catch (Exception e){
                model.addAttribute("result",e.getMessage());
                MailUtil.sendMail(MailUtil.DEFAULT_TARGET,MailUtil.FAILURE_TITLE,"操作代号：03<br>"+e.getMessage());
                return "index";
            }
        }
        model.addAttribute("result","共计"+total+"条数据，其中插入"+count+"条数据");
        MailUtil.sendMail(MailUtil.DEFAULT_TARGET,MailUtil.SUCESS_TITLE,"操作代号：03<br>" +
                        "共计"+total+"条数据<br>"+
                "其中向t_ispMask表中插入了"+count+"条数据<br>");
        return "index";
    }
    private enum Isps{
        DIANXIN("中国电信"),
        LIANTONG("中国移动"),
        YIDONG("中国联通"),
        TIETONG("中国铁通"),
        JIAOYUWANG("中国教育网"),
        KEJIWANG("中国科技网");

        private String isp;
        Isps(String isp) {
            this.isp=isp;
        }

        @Override
        public String toString() {
            return isp;
        }
    }
}
